import {HOST} from './hosts.js';

// 封装GET请求
const req_get = async (method, url, params = {}) => {
    const res = await uni.request({
        url: url,
        method: method,
        data: params,
        header: {
            "Content-Type": "application/json; charset=UTF-8",
        }
    });
    return res.data;
};


// 封装带认证的GET请求
const req_get_auth = async (method, url, params = {}) => {
    const res = await uni.request({
        url: url,
        method: method,
        data: params,
        header: {
            "Content-Type": "application/json; charset=UTF-8",
            'Authorization': uni.getStorageSync('token')
        },
        withCredentials: true
    });
    return res.data;
};

// 封装POST请求
const req_post = async (method, url, data = {}) => {
    const res = await uni.request({
        url: url,
        method: method,
        data: data,
        header: {
            "Content-Type": "application/json; charset=UTF-8",
        },
        withCredentials: true
    });
    return res.data;
};

// 封装带认证的POST请求
const req_post_auth = async (method, url, data = {}) => {
    const res = await uni.request({
        url: url,
        method: method,
        data: data,
        header: {
            "Content-Type": "application/json; charset=UTF-8",
            'Authorization': uni.getStorageSync('token')
        },
        withCredentials: true
    });
};

export {
    HOST,
    req_get,
    req_get_auth,
    req_post,
    req_post_auth,
};
